package com.feihao.zhou.service.story.Impl;

import com.feihao.zhou.bean.story.QuestionItem;
import com.feihao.zhou.factory.MapperFactory;
import com.feihao.zhou.mapper.story.QuestionItemMapper;
import com.feihao.zhou.service.story.QuestionItemService;
import com.feihao.zhou.utils.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class QuestionItemServiceImpl implements QuestionItemService {
    @Override
    public void save(QuestionItem questionItem,String questionId,boolean flag) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            QuestionItemMapper questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemMapper.class);
            //id使用UUID的生成策略来获取
            String id = UUID.randomUUID().toString();
            questionItem.setId(id);
            questionItem.setQuestionId(questionId);
            if (flag){
                //有文件，设置图片：
                questionItem.setPicture(id);
            }
            //3.调用Dao层操作
            questionItemDao.save(questionItem);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /*@Override
    public void save(QuestionItem questionItem,String questionId) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            QuestionItemMapper questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemMapper.class);
            //id使用UUID的生成策略来获取
            String id = UUID.randomUUID().toString();
            questionItem.setId(id);
            questionItem.setQuestionId(questionId);

            //3.调用Dao层操作
            questionItemDao.save(questionItem);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }*/

    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            QuestionItemMapper questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemMapper.class);
            //3.调用Dao层操作
            questionItemDao.delete(id);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(QuestionItem questionItem ,boolean flag) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            QuestionItemMapper questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemMapper.class);
            //3.调用Dao层操作
            if (flag){
                //如果有图片，则更新图片的为题目选项的id(其实也不用，只怕后续更新有上传图片)
                String id = questionItem.getId();
                questionItem.setPicture(id);
            }
            questionItemDao.update(questionItem);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public QuestionItem findById(String id) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            QuestionItemMapper questionItemDao = MapperFactory.getMapper(sqlSession, QuestionItemMapper.class);
            //3.调用Dao层操作
            return questionItemDao.findById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public PageInfo findAll(String questionId,int page, int size) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            QuestionItemMapper questionItemMapper = MapperFactory.getMapper(sqlSession, QuestionItemMapper.class);
            //3.调用Dao层操作
            PageHelper.startPage(page, size);
            List<QuestionItem> all = questionItemMapper.findAll(questionId);
            PageInfo pageInfo = new PageInfo(all);
            return pageInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
